package dhm.com.hpx;

import android.content.Context;
import android.os.Handler;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import dhm.com.hpx.adapter.holder.MenuHelper;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.utils.ContextUtil;
import dhm.com.hpx.utils.UIUtils;

public class App extends MultiDexApplication {


    public static int NET_STATUS = -1;
    /** 主线程ID */
    private static Thread mMainThread;
    /**
     * context 全局唯一的上下文
     */
    private static Context context;
    /** 主线程Handler */
    private static Handler mMainThreadHandler;
    /** 主线程ID */
    private static int mMainThreadId = -1;

    public IWXAPI msgApi;

    public static UIUtils getApplication() {
        return App.getApplication();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        // 默认本地个性化地图初始化方法
        SDKInitializer.initialize(this);

        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
        context = this;
        mMainThreadHandler = new Handler();
        mMainThread = Thread.currentThread();
        mMainThreadId = android.os.Process.myTid();

        MultiDex.install(this) ;

        ContextUtil.init(getApplicationContext());
        if(!MenuHelper.hasEverInit()){
            MenuHelper.init();
        }

        msgApi = WXAPIFactory.createWXAPI(this, Constant.Wx_APP_ID,false);
        msgApi.registerApp(Constant.Wx_APP_ID);

    }



    public static Context getContext() {
        return context;
    }

    /** 获取主线程 */
    public static Thread getMainThread() {
        return mMainThread;
    }
    /** 获取主线程ID */

    /** 获取主线程ID */
    public static int getMainThreadId() {
        return mMainThreadId;
    }
    /** 获取主线程的handler */
    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.white, android.R.color.black);//全局设置主题颜色
                return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }

}
